/**
 * Copyright (c) 2013 An Yaming,  All Rights Reserved
 */
package so.shanku.essential.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.text.TextUtils;
import android.widget.ListView;
import net.tsz.afinal.annotation.view.ViewInject;
import so.shanku.essential.R;
import so.shanku.essential.activity.base.BaseUIActivity;
import so.shanku.essential.adapter.PaymentListAdapter;
import so.shanku.essential.getdata.*;
import so.shanku.essential.utils.Constant;
import so.shanku.essential.utils.ExtraKeys;
import so.shanku.essential.utils.LogUtil;
import so.shanku.essential.view.GridViewNoScroll;
import so.shanku.essential.view.ListViewNoScroll;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import net.tsz.afinal.json.JsonMap;
import net.tsz.afinal.json.JsonParseHelper;

/**
 * @author tanghuan
 * @ClassName: ShoppingZhifuPeisongWayActivity
 * @Description: TODO(支付与配送方式类)
 * @date 2014年12月3日 下午7:53:40
 */
public class PayMethodActivity extends BaseUIActivity {
    /**
     * 支付方式
     */
    @ViewInject(id = R.id.shopping_addorder_gvns_payment)
    private ListView gvns_payment;
    /**
     * 配送方式的选择的数据源
     */
    private List<JsonMap<String, Object>> data_peisong = new ArrayList<JsonMap<String, Object>>();
    /**
     * 确认按钮
     */
    @ViewInject(id = R.id.u_i_u_bt_config, click = "config")
    private Button u_bt_config;

    private PaymentListAdapter adapter;


    /**
     * 选中的支付方式
     */
    private int selectid = 0;
    private String orderNum;
    private String payName;
    private int payid = Constant.ALIPAY_PAYID;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shopping_zhifu_peisong_way);
        // setCenter_title(R.string.shopping_zhifu_peisong_way_text_title);
        setCenter_title(R.string.shopping_zhifu_peisong_way_text_payway1);
        gvns_payment.setOnItemClickListener(onItemClickListener);
        orderNum = getIntent().getStringExtra(ExtraKeys.Key_Msg2);
        selectid = payid - 1;
        getData_zhifupeisong();

    }

    /**
     * @Title: list_mlv
     * @Description: TODO(方法的作用：列表点击事件)
     */
    AdapterView.OnItemClickListener onItemClickListener = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            selectid = position;
            adapter.setSelected(position);
            adapter.notifyDataSetChanged();
        }
    };


    /**
     * 获取服务器支付和配送新
     */
    private void getData_zhifupeisong() {
        JsonMap jsonMap = new JsonMap();
        jsonMap.put("payName", getString(R.string.pay_alipay));
        jsonMap.put("payId", Constant.ALIPAY_PAYID);
        jsonMap.put("payLogo", R.drawable.pay_zhifubao);
        data_peisong.add(jsonMap);
        jsonMap = new JsonMap();
        jsonMap.put("payName", getString(R.string.pay_wechat));
        jsonMap.put("payId", Constant.WEICHAT_PAYID);
        jsonMap.put("payLogo", R.drawable.pay_weichat);
        data_peisong.add(jsonMap);
        jsonMap = new JsonMap();
        jsonMap.put("payName", getString(R.string.pay_unionpay));
        jsonMap.put("payId", Constant.UNION_PAYID);
        jsonMap.put("payLogo", R.drawable.pay_union);
        data_peisong.add(jsonMap);
        jsonMap = new JsonMap();
        jsonMap.put("payName", getString(R.string.pay_cash));
        jsonMap.put("payId", Constant.CASH_PAYID);
        jsonMap.put("payLogo", R.drawable.pay_zhifubao);
        data_peisong.add(jsonMap);

        adapter = new PaymentListAdapter(this, data_peisong);
        adapter.setSelected(selectid);
        gvns_payment.setAdapter(adapter);

    }


    public void config(View v) {
        if (selectid >= 0) {
            JsonMap<String, Object> selectInfo = data_peisong.get(selectid);
            payName = selectInfo.getStringNoNull("payName");
            payid = selectInfo.getInt("payId");
            if (!TextUtils.isEmpty(orderNum)) {
                loadingToast.show();

                Map<String, Object> params = new HashMap<String, Object>();

                params.put("orderNum", orderNum);
                params.put("payId", payid);
                GetDataQueue queue = new GetDataQueue();
                queue.setCallBack(callBack);
                queue.setActionName(GetDataConfing.Action_GetOrderByOrderNumUpdatePayType);
                queue.setWhat(GetDataConfing.What_GetOrderByOrderNumUpdatePayType);
                queue.setParamsNoJson(params);
                getDataUtil.getData(queue);
            } else {
                setPayConfigSuccess();
            }

        }

    }

    /**
     * 获取服务器数据的返回信息
     */
    private IGetServicesDataCallBack callBack = new IGetServicesDataCallBack() {
        @Override
        public void onLoaded(GetDataQueue entity) {
            if (entity.isOk()) {
                if (ShowGetDataError.isOkAndCodeIsNot1(mContext,
                        entity.getInfo())) {
                    if (entity.what == GetDataConfing.What_GetOrderByOrderNumUpdatePayType) {
                        setPayConfigSuccess();
                    }
                }
            } else {
                ShowGetDataError.showNetError(mContext);
            }
            loadingToast.dismiss();
        }
    };

    private void setPayConfigSuccess() {

        Intent intent = new Intent();
        intent.putExtra(ExtraKeys.Key_Msg1, payName);
        intent.putExtra(ExtraKeys.Key_Msg2, payid);
        intent.putExtra(ExtraKeys.Key_Msg3, selectid);
        LogUtil.d(TAG, "selected pay id:" + payid);

        setResult(RESULT_OK, intent);
        finish();
    }

}
